jQuery Select Value

您所在的位置:网站首页 jquery to get selected value from dropdown jQuery Select Value

jQuery Select Value

2023-09-07 19:26| 来源: 网络整理| 查看: 265

Updated April 19, 2023

jQuery-Select-Value

Introduction to jQuery Select Value

jQuery select value lets you get the value of the selected option from the select boxes and dropdowns. jQuery offers two methods, val() and text()to achieve this. jQuery: selected selector is used in combination with the val() or text() method to find the value for the selected option in a select box or dropdown. Using val() method, we get the value to be sent to the server and with text() method, we get the text value of the selected element.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

1. Using val() method: val() method is used to return the value of the selected element

$("#selectedId option : selected").val()

2. Using text() method: text() method is used to return the text value (string) of the selected element

$("#selectedId option : selected").text()

where,val() and text() do not accept any arguments.

How does jQuery Select Value Work?

Select elements basically have two values that can be accessed. The first value is to be sent to the server, which we can get using val() The second value is the text value of the selected element which we can get using text() method.

English

Where, books is the value returned by val() method. Englishis the value returned by text() method.

Whenever an items gets selected in the dropdown, jQuery OnChange event handler gets executed where val() or text() method fetches the value of the selected item and then perform various operations over them. Examples for jQuery Select Value

Let us go through few examples to understand how to get the select values from select boxes using jQuery.

Example #1

This example demonstrates how jQuery select value actually works with val() method.

Code:

jQuery Get Selected Option Value $(document).ready(function () { $("#SubjectsList").change(function () { var selectedSubject = $("#SubjectsList option:selected").val(); alert("You have selected the country - " + selectedSubject); }); }); #divstyle { width: 500px; height: 250px; padding-top: 20px; padding-left: 5px; font-size: 24px; text-align: center; color: maroon; background-color: cadetblue; } jQuery select value using val() method Select: English Physics Chemistry Mathematics

Output:

Below screen displays when the above code gets executed. By default, we have the selected option as “English” in the given dropdown.

jQuery Select Value-1.1

Now, we change the selected option as “Chemistry”. We use jQuery :selected with val() method to find the selected value in the dropdown list.

jQuery Select Value-1.2

On doing this, OnChange event handler executes where the value of the selected option is fetched and displayed in the alert box a shown below.

jQuery Select Value-1.3

Now, we have the selected option as “Chemistry”.

jQuery Select Value-1.4

Example #2

This example demonstrates how jQuery select value actually works with text() method.

Code:

jQuery Get Selected Option Value $(document).ready(function () { $("#SubjectsList").change(function () { var selectedSubject = $("#SubjectsList option:selected").text(); alert("You have selected the country - " + selectedSubject); }); }); #divstyle { width: 500px; height: 250px; padding-top: 20px; padding-left: 5px; font-size: 24px; text-align: center; color: maroon; background-color: cadetblue; } jQuery select value using text() method Select: English Physics Chemistry Mathematics

Output:

Below screen displays when the above code gets executed. By default, the selected option is “English” in the given dropdown.

jQuery Select Value-2.1

Now, we change the selected option as “Chemistry”. We use jQuery :selected with text() method to find the selected value in the dropdown list.

jQuery Select Value-2.2

On doing this, OnChange event handler executes where the value of the selected option is fetched and displayed in the alert box shown below.

jQuery Select Value-2.3

Now, the selected option is “Chemistry”.

Output-2.4

Example #3

This example demonstrates how the selected values can be retrieved from multiple select boxes using jQuery.

Code:

jQuery select values from Multiple Select Box $(document).ready(function () { $("button").click(function () { var multipleValues = $( "#subj" ).val() || []; $( "p" ).html(" You selected: " + multipleValues.join( ", " ) ); }); }); #divstyle { width: 500px; height: 350px; padding-top: 20px; padding-left: 5px; font-size: 24px; text-align: center; color: maroon; background-color: cadetblue; } jQuery select values from Multiple Select Box English Maths Physics Chemistry History Geography Click

Output:

Below screen displays when the code above gets executed. We can see a multiple select box which allows users to make multiple selections.

Output-3.1

Now, holding down the control key (ctrl) on Windows and command key on Mac, one can make multiple selections as shown below.

Output-3.2

Multiple selection in a select box is enabled by adding “multiple” attribute to the Example #4

Given below is another similar example that demonstrates how the selected values can be retrieved from multiple select boxes using jQuery.

Code:

jQuery select values from Multiple Select Box $(document).ready(function () { $("button").click(function () { var selectedValues = []; $.each($("#subj option:selected"), function () { selectedValues.push($(this).val()); }); alert("Your favorite website is - " + selectedValues.join(", ")); }); }); #divstyle { width: 500px; height: 350px; padding-top: 20px; padding-left: 5px; font-size: 24px; text-align: center; color: maroon; background-color: cadetblue; } jQuery select values from Multiple Select Box English Maths Physics Chemistry History Geography Click

Output:

Below screen displays when the code above gets executed.

Output-4.1

Here, val() method returns an array with the values of each option selected. If no option selected, the method returns an empty array.

Output-4.2

Output-4.3

Conclusion In this article, we discussed about how to get the values of selections in a select box using jQuery. jQuery offers two methods, val() and text() for this. val() method returns the value of the selected option. text() method returns the text value of the selected option Recommended Articles

This is a guide to jQuery Select Value. Here we also discuss the introduction and how does jquery select value work? along with different examples and its code implementation. You may also have a look at the following articles to learn more –

jQuery Widgets JQuery Modal jQuery Reset Form jQuery Slider GOLANG Course Bundle - 6 Courses in 1 23+ Hours of HD Videos 6 Courses Verifiable Certificate of Completion Lifetime Access 4.5 iOS DEVELOPER Course Bundle - 61 Courses in 1 147+ Hours of HD Videos 61 Courses Verifiable Certificate of Completion Lifetime Access 4.5 JAVA SERVLET Course Bundle - 18 Courses in 1 | 6 Mock Tests 56+ Hours of HD Videos 18 Courses 6 Mock Tests & Quizzes Verifiable Certificate of Completion Lifetime Access 4.5 RED HAT LINUX Course Bundle - 5 Courses in 1 28+ Hours of HD Videos 5 Courses Verifiable Certificate of Completion Lifetime Access 4.5


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3